package xutil

import (
	"strconv"
	"strings"
	"time"
)

const (
	TimeDifference = 28800 //相差8小时的这个秒数
)

func F两个时间切割并转换成时间戳(str string) (start, end int64) {
	//ti := "2022/09/08 00:00:00,2022/10/07 20:42:12"
	if str != "" {
		at := strings.Split(str, ",")
		loca := "2006/01/02 15:04:05"
		start = F时间按格式转成时间戳(at[0], loca)

		end = F时间按格式转成时间戳(at[1], loca)
		//如果时间异常，那么处理一下
		if start < 1 {
			start = 0
		}
		if end < 1 {
			end = 0
		} else {
			end += 3600*24 - 1 //结束时间加一天少一秒
		}
	}
	return
}

func F获取时间戳字符串() string {
	tm := time.Now().Unix()
	s := strconv.Itoa(int(tm))
	return s
}

func F时间戳转时间(i int64) string {
	return time.Unix(i, 0).Format("2006-01-02 15:04:05")
}

func F时间按格式转成时间戳(str ...string) int64 {
	loca := "2006-01-02 15:04:05"
	if len(str) == 2 {
		loca = str[1]
	}
	startTime, _ := time.ParseInLocation(loca, str[0], time.Local)
	t := startTime.Unix() - TimeDifference //由于时区的原因,这里相差8小时,去除一下,具体到时候再获取一个看看是否有误差
	return t
}

func F获取指定月后会员时间(ti int64, nmon int) (rti int64) {
	rti = GetMonth(-1 * nmon)
	当前时间 := time.Now().Unix()
	if ti > 当前时间 { //如果结果时间大于当前时间，那么就加上相差的时间
		rti = rti + ti - 当前时间
	}
	return
}

// 获取月前的时间戳
func GetMonth(i int) int64 {
	return time.Now().AddDate(0, -1*i, 0).Unix()
}

// 获取日前时间戳
func F获取几天前日期时间戳(i int) int64 {
	return time.Now().AddDate(0, 0, -1*i).Unix()
}

func F获取多少月前当天0点时间戳(m, d int) int64 {
	currentYear, currentMonth, day := time.Now().AddDate(0, m, d).Date()
	return time.Date(currentYear, currentMonth, day, 0, 0, 0, 0, time.Now().Location()).Unix()
}
func F获取多少月前当天23点时间戳(m, d int) int64 {
	currentYear, currentMonth, day := time.Now().AddDate(0, m, d).Date()
	return time.Date(currentYear, currentMonth, day, 23, 59, 59, 0, time.Now().Location()).Unix()
}

// 获取当前时间戳的字符串
func GetTimeStampString() string {
	tm := time.Now().Unix()
	s := strconv.Itoa(int(tm))
	return s
}

func F获取三天后的几个月后日期(num int) (i int64) {
	return F获取几个月后的当天(num, 3)
}

func F获取当天24小时时间戳() (start, end int64) {
	return F获取多少天前到今天24点的时间戳(0)
}

func F判断时间戳是否在多少天内(ti int64, day int) bool {
	now := time.Now().Unix() - ti
	return now < int64(day*3600*24)
}

func F获取多少天前到多少天后的时间戳(i, e int64) (start, end int64) {
	addd := int(-1 * i)
	currentYear, currentMonth, day := time.Now().AddDate(0, 0, addd).Date()
	当前时间 := time.Date(currentYear, currentMonth, day, 0, 0, 0, 0, time.Now().Location())
	start = 当前时间.Unix()
	end = start + 3600*24*(e+1)
	return
}

func F获取多少天前到今天24点的时间戳(i int64) (start, end int64) {
	return F获取多少天前到多少天后的时间戳(i, i)
}

// 1-12，所以添加的日期需要-1，如果日期为31，那么判断是否为1，3，5，7，8，10，12，否则就是30(2,4,6,9,11)
func F获取几个月后的当天(num, 延后时间 int) (i int64) {
	now := time.Now().AddDate(0, 0, 延后时间)
	currentYear, currentMonth, day := now.Date()
	currentLocation := now.Location()

	d := day
	ti := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation).AddDate(0, (num - 1), 0)
	month := ti.Format("01")
	if month == "02" && d > 28 { //如果月份是2月，那么
		d = 28
	}
	//如果是31号，那么就是30
	if d == 31 && (month == "04" || month == "06" || month == "09" || month == "11") {
		d = 30
	}
	t1 := time.Date(currentYear, currentMonth, d, 0, 0, 0, 0, currentLocation).AddDate(0, (num - 1), 0)
	i = t1.Unix()
	return
}

func F获取每个月的当天时间(num int) (nums []int64) {
	times := F获取每个月当天时间(num)
	for o := 0; o < num; o++ {
		nums = append(nums, times[o].Unix())
	}
	return
}

func F获取以多少天为单位间隔日期(day int, num int) (itm []int64) {
	for count := 0; count <= num; count++ { //省略分号
		mi := time.Now().AddDate(0, 0, day*count)
		//fmt.Println(mi.Format("2006-1-2 15:04:05"))
		itm = append(itm, mi.Unix())
	}
	return
}

func F获取每个月当天时间(num int) (times []time.Time) {
	now := time.Now()
	currentYear, currentMonth, day := now.Date()
	mon := 0
	if day > 28 {
		day = 1
		mon = 1
	}
	for o := 0; o < num; o++ {
		//nums = append(nums, t1.Unix())
		times = append(times, time.Date(currentYear, currentMonth, day, 0, 0, 0, 0, now.Location()).AddDate(0, (mon+o), 0))
		//fmt.Println(t1.Format("2006-01-02 15:04:05"))
	}
	return
}

func F获取时间戳后的几个月时间(timestmap int64, num int) int64 {
	t := time.Unix(timestmap, 0)
	currentYear, currentMonth, day := t.Date()
	times := time.Date(currentYear, currentMonth, day, 0, 0, 1, 0, time.Now().Location()).AddDate(0, num, 0)
	return times.Unix()
}

func F时间戳转年月日时分秒(ti ...int64) string {
	if len(ti) != 0 {
		return time.Unix(ti[0], 0).Format("2006-01-02 15:04:05")
	}
	return time.Now().Format("2006-01-02 15:04:05")
}
func F时间戳转年月日(ti ...int64) string {
	if len(ti) != 0 {
		return time.Unix(ti[0], 0).Format("2006-01-02")
	}
	return time.Now().Format("2006-01-02")
}

func F时间戳格式化(i int64, s ...string) (ss string) {
	格式化 := "2006-01-02 15:04:05"
	if len(s) != 0 {
		格式化 = s[0]
	}
	return time.Unix(i, 0).Format(格式化)
}

func F年月日转时间戳(s string) int64 {
	t, err := time.ParseInLocation("2006-01-02 15:04:05", s, time.Local)
	if err != nil {
		return 0
	}
	return t.Unix()
}

func F开始到结束每天时间戳(s, e int64) []string {
	var ss []string
	for i := s; i <= e; i = i + 3600*24 {
		ss = append(ss, F时间戳格式化(i, "01-02"))
	}
	return ss
}

func F获取是第几个月(ti int64) (i int8) {
	now := time.Now()
	nowyear, nowmonth, nowday := now.Date()

	year1, month1, day1 := time.Unix(ti, 0).Date()
	i = int8(nowyear-year1)*12 + int8(nowmonth-month1)
	if nowday > day1 {
		i++
	}
	if i == 0 {
		i = 1
	}
	return
}

func F生日转年龄(birthday string) (age int) {
	year, _ := strconv.Atoi(birthday[:4])
	month, _ := strconv.Atoi(birthday[4:6])

	ti := time.Now()

	//当前年月
	year1 := ti.Year()
	month1, _ := strconv.Atoi(ti.Format("01"))
	age = year1 - year
	if month > month1 {
		age -= 1
	}
	return
}
